$(document).ready(function() {
  var eventTrigger = false;
  var lastElement  = null;
  var hoverLabel   = null;
  
  $('.hover-label').hover(function() {
    hoverLabel = $(this);
  });
  
  $('.product-image-re').hover(function() {
    if(!eventTrigger) {
      var position = $(this).position();
      var ob = {
        big   : $('#overlay-' + $(this).attr('id')),
        small : $(this)
      }
      
      ob.big.css({
        top     :  (((position.top + ob.small.height()) - (ob.big.height()) + 40)  + 'px'),
        left    :  (position.left - ((ob.big.width() - ob.small.width()) / 2) + 'px'),
        display :  'block'
      });
      
      // Change TR background
      hoverLabel.css({
        background : 'url(/web/img/toote_bg.png) repeat-x'
      });
        
      eventTrigger = true;
      lastElement  = ob.big;
      
      updateLastElement();
    }
  });
  
  function updateLastElement() {
    if(lastElement) {
      $(lastElement).mouseleave(function() {
        if(eventTrigger) {
          eventTrigger = false;
          $(this).css({display: 'none'});
          hoverLabel.css({
            background : ''
          });
        }
      });
    }
  }
});
